package com.base.controller;

import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.base.comm.CommContent;
import com.base.po.voip.NewsAllocation;
import com.base.service.LogsService;
import com.base.service.NewsAllocationService;
import com.base.utils.IP;
import com.base.utils.Produce;

/**
 * admin管理系统控制器
 * 
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/admin")
public class content_xinwenfenlei {
	@Autowired
	private LogsService logsService;// log日志
	@Autowired
	private NewsAllocationService newsAllocationService;

	@RequestMapping("/content_xinwenfenlei")
	public ModelAndView index(HttpServletRequest request) throws Exception {
		// 返回ModelAndView
		ModelAndView modelAndView = new ModelAndView();
		// 登录的用户ID 以及操作权限
//		Integer adminUsergrade = (Integer) request.getSession().getAttribute("adminUsergrade");
		String adminUserId = (String) request.getSession().getAttribute("adminUserId");
		
		NewsAllocation newsAllocation = newsAllocationService.selectByParentid(adminUserId);
		if(newsAllocation != null) {
			request.setAttribute("newsAllocation", newsAllocation);
		}else {
			int code = newsAllocationService.insertSelective(new NewsAllocation(Produce.getUUID(),
					adminUserId, (byte)1, (byte)1, (byte)1, (byte)1, (byte)1, (byte)1,
					(byte)1, (byte)1, (byte)1, (byte)1, (byte)1, (byte)1, (byte)1, 
					(byte)1, (byte)1, (byte)1, (byte)1, (byte)1, 1));
			if(code == 1) {
				newsAllocation = newsAllocationService.selectByParentid(adminUserId);
				request.setAttribute("newsAllocation", newsAllocation);
			}else {
				modelAndView.setViewName("jsp/datanotfound");
				return modelAndView;
			}
		}
		
		modelAndView.setViewName("jsp/content_xinwenfenlei");
		return modelAndView;
	}
	
	String SUCCESS = "success";
	@RequestMapping(value = "/updateNewsAllocation", method = RequestMethod.POST)
	public void updateNewsAllocation(HttpServletRequest request, HttpServletResponse response,
			Byte shehui, Byte guonei,Byte guoji, Byte yule, Byte tiyu, Byte junshi,
			Byte keji, Byte caijing, Byte shishang, Byte youxi, Byte qiche, Byte xiaohua, 
			Byte jiankang,Byte jiaju, Byte lishi, Byte xingzuo, Byte zatan, 
			Integer inserttime) throws Exception {
		// 返回参数
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=UTF-8");
		String parent = (String) request.getSession().getAttribute("adminUserId");
		
        NewsAllocation newsAllocation = newsAllocationService.selectByParentid(parent);
        
		PrintWriter out = response.getWriter();
			if (parent == null) {
				out.print("浏览器在点击的过程中发现丢失了用户信息，请您刷新重试！");
				return;
			} else {
				newsAllocation.setShehui(shehui);
				newsAllocation.setGuonei(guonei);
				newsAllocation.setGuoji(guoji);
				newsAllocation.setYule(yule);
				newsAllocation.setTiyu(tiyu);
				newsAllocation.setJunshi(junshi);
				newsAllocation.setKeji(keji);
				newsAllocation.setCaijing(caijing);
				newsAllocation.setShishang(shishang);
				newsAllocation.setYouxi(youxi);
				newsAllocation.setQiche(qiche);
				newsAllocation.setXiaohua(xiaohua);
				newsAllocation.setJiankang(jiankang);
				newsAllocation.setJiaju(jiaju);
				newsAllocation.setLishi(lishi);
				newsAllocation.setXingzuo(xingzuo);
				newsAllocation.setZatan(zatan);
				newsAllocation.setInserttime(inserttime);
				int code = newsAllocationService.updateByPrimaryKeySelective(newsAllocation);
			
				String usernamesys = (String) request.getSession().getAttribute("useracc");
                 
				if (code == 1) {
					logsService.insert(CommContent.TYPE_DELET, "用户：" + usernamesys + "修改新闻分类成功", usernamesys,true, IP.getIpAddr(request));
					out.print(SUCCESS);
				} else {
					logsService.insert(CommContent.TYPE_DELET, "用户：" + usernamesys + "修改新闻分类失败", usernamesys,true, IP.getIpAddr(request));
					out.print("更改失败，请您重试");
				}
		}
	}
}
	
